package org.maxgroundstation.groundstation.view.map;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Stroke;
import java.awt.geom.AffineTransform;

import org.maxgroundstation.groundstation.GroundStation;
import org.maxgroundstation.groundstation.comm.service.gps.GpsService;

/**
 * A map overlay for displaying the rover's heading direction.
 * @author Scott Davis
 * 
 * Copyright (C) 2010  Scott Davis
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
public class RoverHeadingMapOverlay implements MapOverlay {

    // The overlay ID.
    public static final String OVERLAY_ID = "Rover's Heading";
    private static final Color FG_COLOR = Color.GREEN;
    private static final Color BG_COLOR = new Color(0, 0, 0, 127);
    private static final Stroke FG_STROKE = new BasicStroke(1F);
    private static final Stroke BG_STROKE = new BasicStroke(2F);
    
    // Data members.
    private GroundStation gs;
    
    /**
     * Constructor
     * @param gs the ground station.
     */
    public RoverHeadingMapOverlay(GroundStation gs) {
        
        // Initialize data members.
        this.gs = gs;
    }
    
    @Override
    public void drawMapOverlay(String baseMapStyle, Graphics2D g2d, GpsLocation currentLoc, int width, 
            int height, float zoom) {
        
        int xCenter = width / 2;
        int yCenter = height / 2;
        
        if (gs.getServiceComm().hasService(GpsService.SERVICE_ID)) {
            
            AffineTransform saveTransform = g2d.getTransform();
            Stroke saveStroke = g2d.getStroke();
         
            GpsService gpsService = (GpsService) gs.getServiceComm().getService(
                    GpsService.SERVICE_ID);
            String headingString = gpsService.getGpsMap().get("heading");
            if (headingString != null) {
                // Determine proper angle rotation transformation.
                double heading = Double.parseDouble(headingString);
                double headingRad = heading * Math.PI / 180D;
                g2d.rotate(headingRad, xCenter, yCenter);
            
                // Draw heading arrow.
                g2d.setColor(BG_COLOR);
                g2d.setStroke(BG_STROKE);
                g2d.drawLine(xCenter, yCenter - 8, xCenter, yCenter - 18);
                g2d.drawLine(xCenter, yCenter - 18, xCenter - 8, yCenter - 10);
                g2d.drawLine(xCenter, yCenter - 18, xCenter + 8, yCenter - 10);
                
                g2d.setColor(FG_COLOR);
                g2d.setStroke(FG_STROKE);
                g2d.drawLine(xCenter, yCenter - 8, xCenter, yCenter - 18);
                g2d.drawLine(xCenter, yCenter - 18, xCenter - 8, yCenter - 10);
                g2d.drawLine(xCenter, yCenter - 18, xCenter + 8, yCenter - 10);
            }
        
            g2d.setTransform(saveTransform);
            g2d.setStroke(saveStroke);
        }
    }

    @Override
    public boolean isDraggable() {
        return true;
    }
}